package com.fyzk.raceview;

import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.bean.Race;
import com.fyzk.bean.Race_Content;
import com.fyzk.bean.ZhuiHaoDetail;
import com.fyzk.constants.ConstantsDJ;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;


public class RaceJingCaiView extends RaceBaseView{
	
	private Context context;
	private List<Race> races;

	@Override
	public View getRaceView(Context context, String content, List<Race> races, String prizeNum) {
		this.context = context;
		if (races == null) {
			races = new ArrayList<Race>();
		}
		this.races = races;
		
		allNum = races.size();
		
		View view = LayoutInflater.from(context).inflate(R.layout.zc_xiangxi, null);
		TextView tv_touZhuInfor = (TextView) view.findViewById(R.id.zc_xiangxi_touzhu_infor);
		LinearLayout ll_touZhuInfor = (LinearLayout) view.findViewById(R.id.zc_xiangxi_ll_neirong);
		
		ll_touZhuInfor.addView(getItemView(null));//表头
		if (races != null && races.size() > 0) {
			for (int i = 0; i < races.size(); i++) {
				Race race = races.get(i);
				
				if (i < num) {
					ll_touZhuInfor.addView(getItemView(race));
				}else if(i == num){
					ll_touZhuInfor.addView(toNet("查看更多，请到网站进行查看"));
				}
			}
			tv_touZhuInfor.setText(races.size() + "场 - " + getGuoGuan(content));
		}else{
			ll_touZhuInfor.addView(toNet("请到网站进行查看"));
		}
		
		return view;
		
	}
	
	private TextView toNet(String str){
		TextView textView = new TextView(context);
		textView.setText(
                 Html.fromHtml("<a href=\"http://www.dajiang365.com\">"+str+"</a> "));
		textView.setMovementMethod(LinkMovementMethod.getInstance());
		textView.setTextColor(Color.BLUE);
		textView.setGravity(Gravity.CENTER);
		textView.setBackgroundColor(Color.WHITE);
		
		return textView;
	}
	
	private View getItemView(Race race){
//		View itemView = LayoutInflater.from(context).inflate(R.layout.jc_xiangxi_item, null);
//		TextView id = (TextView) itemView.findViewById(R.id.zc_xiangxi_item_id);
//		TextView week = (TextView) itemView.findViewById(R.id.zc_xiangxi_item_week);
//		TextView home = (TextView) itemView.findViewById(R.id.zc_xiangxi_item_home);
//		TextView away = (TextView) itemView.findViewById(R.id.zc_xiangxi_item_away);
//		TextView score = (TextView) itemView.findViewById(R.id.zc_xiangxi_item_score);
//		LinearLayout title = (LinearLayout) itemView.findViewById(R.id.zc_xiangxi_item_title);
//		LinearLayout main = (LinearLayout) itemView.findViewById(R.id.zc_xiangxi_item_main);
//		LinearLayout content = (LinearLayout) itemView.findViewById(R.id.zc_xiangxi_item_content_ll);
//
//		if (race != null) {
//			title.setVisibility(View.GONE);
//			content.setVisibility(View.VISIBLE);
//
//			id.setValue(race.id);
//			week.setValue(race.week);
//			home.setValue(race.home);
//			away.setValue(race.away);
//
//			boolean isRangQiu = false;
//			if (race.race_contents != null && race.race_contents.size() > 0) {
//				for (int i = 0; i < race.race_contents.size(); i++) {
//					if (!isRangQiu && race.race_contents.get(i).wcode.contains("让")) {
//						isRangQiu = true;
//					}
//					content.addView(getItemView_content(race.race_contents.get(i), (i == race.race_contents.size() - 1)));
//				}
//			}
//			if (!race.letVs_letPoint.equals("")&&isRangQiu) {
//				score.setValue("VS("+race.letVs_letPoint+")");
//			}
//		}else{
//			title.setVisibility(View.VISIBLE);
//			main.setVisibility(View.GONE);
//		}
//
		return new TextView(context);
	}
	
	private View getItemView_content(Race_Content r_content, boolean isWei){
		View itemView = LayoutInflater.from(context).inflate(R.layout.jc_xiangxi_item_content, null);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
		
		TextView wcode = (TextView) itemView.findViewById(R.id.zc_xiangxi_item_wcode);
		LinearLayout bet_ll = (LinearLayout) itemView.findViewById(R.id.zc_xiangxi_item_bet_ll);
		TextView result = (TextView) itemView.findViewById(R.id.zc_xiangxi_item_result);
		TextView line = (TextView) itemView.findViewById(R.id.zc_xiangxi_item_line);
		
		
		
//		if (r_content != null) {
//			wcode.setValue(r_content.wcode);
//			String[] bets = null;
//			if (r_content.bet != null && !r_content.bet.equals("")) {
//				if (r_content.bet.contains(",")) {
//					bets = r_content.bet.split(",");
//				}else{
//					bets = new String[]{r_content.bet};
//				}
//			}
//			if (r_content.result == null || r_content.result.equals("") || r_content.result.equals("null")) {
//				result.setValue("--");
//			}else{
//				result.setValue(r_content.result);
//			}
//
//			if (bets != null) {
//				for (int i = 0; i < bets.length; i++) {
//					bet_ll.addView(getTextView(bets[i], result));
//				}
//			}
//
//
//			if (isWei) {
//				line.setVisibility(View.GONE);
//			}
//		}else{
//
//		}
		
		itemView.setLayoutParams(lp);
		return itemView;
	}
	
	private TextView getTextView(String bet, TextView tv_result){
		int color_prize = context.getResources().getColor(R.color.ssq_up_hong);
		int color_prize_no = context.getResources().getColor(R.color.zc_bf_left);
		TextView tv = new TextView(context);
//		tv.setSingleLine(true);
		tv.setWidth(LayoutParams.WRAP_CONTENT);
		tv.setGravity(Gravity.CENTER);
		String betCode = "";
		if(bet.contains("(")){
			betCode = bet.split("\\(")[0];
		}
		
		if (betCode.equals(tv_result.getText().toString())) {
			tv.setTextColor(color_prize);
			tv_result.setTextColor(color_prize);
		}else{
			tv.setTextColor(color_prize_no);
			tv_result.setTextColor(color_prize_no);
		}
		tv.setText(bet+" ");
		
		return tv;
	}
	
	/**
	 * 获取过关方式
	 * @param content  例：301113001-...
	 * @return
	 */
	private String getGuoGuan(String content){
		String guoGuan = "";
		if (content == null || content.equals("")) {
			return guoGuan;
		}
		if (content.contains("!")) {
			String [] contents = content.split("!");
			for (int i = 0; i < contents.length; i++) {
				String guoGuanId = contents[i].substring(5, 9);
				guoGuan += ConstantsDJ.guoGuan(guoGuanId) + ",";
			}
			guoGuan = guoGuan.substring(0, guoGuan.length() - 1);
		}else{
			String guoGuanId = content.substring(5, 9);
			guoGuan = ConstantsDJ.guoGuan(guoGuanId);
		}
		return guoGuan;
	}

	@Override
	public View getZhuiHaoRaceView(Context context, String content, List<ZhuiHaoDetail> detail) {
		// TODO Auto-generated method stub
		return null;
	}

}
